package com.openapi.common;

import com.common.exception.ServiceException;
import com.common.exception.enums.ConstantExceptionEnums;
import org.slf4j.Logger;

import java.io.Serializable;
import java.util.Map;

/**
 * json 响应返回的数据
 */
@SuppressWarnings("all")
public class JsonReturnData<T> implements Serializable {

    private ConstantExceptionEnums constantExceptionEnums;

    public JsonReturnData(ConstantExceptionEnums constantExceptionEnums) {
        this.code = constantExceptionEnums.getCode();
        this.msg = constantExceptionEnums.getMessage();
    }

    /**
     * 响应吗
     */
    private int code;
    /**
     * 响应消息
     */
    private String msg;


    /**
     * json 对象
     */
    private T data;

    /**
     * json数据map  额外数据
     */

    private Map<String, Object> jsonMap;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Map<String, Object> getJsonMap() {
        return jsonMap;
    }

    public void setJsonMap(Map<String, Object> jsonMap) {
        this.jsonMap = jsonMap;
    }

    public JsonReturnData() {
    }

    public static JsonReturnData<Object> error(Exception e, Logger log) {
        JsonReturnData<Object> r = new JsonReturnData<>();
        if (e instanceof ServiceException) {
            ServiceException e1 = (ServiceException) e;

            r.setCode(e1.getCode());
            r.setMsg(e1.getMsg());
            return r;
        }
        log.error("接口调用异常:" + e.getMessage());
        r.setCode(WebConstant.ERRORCODE);
        r.setMsg("系统错误！请联系管理员！");
        return r;
    }

    public JsonReturnData(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "JsonReturnData{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data='" + data + '\'' +
                ", jsonMap=" + jsonMap +
                '}';
    }
}

